<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'form/gform_ctrl_with_obj.class.php');

require_once('ci_article_form.class.php');
require_once('ci_article_form_view.class.php');

require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/ci/ci_types/tcms_ci_article_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpt_ci_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_ci_dbc.class.php');
require_once($WSC->get_site_inc_dir().'modules/tangra_cms/classes/vpage/tcms_vpage_dbc.class.php');


class CI_Article_Form_Ctrl extends GForm_Ctrl_With_Object {
	private $attributes;

	function __construct($system_name, Vars_Manager $vm, DB_Connection $dbc, $object_id_capture = 'obj_id', $attributes) {
		$this->attributes = $attributes;
		parent::__construct($system_name, $vm, $dbc, $object_id_capture);
	}


	protected function create_form() {
		$form = new CI_Article_Form($this->attributes);

//		$form->generate_html();
		return $form;
	}


	protected function create_form_view() {
		$ret = new CI_Article_Form_View($this->form);

		return $ret;
	}


	protected function on_good_submit() {
		$ret = true;

		$form = $this->get_form();
		if ($form->get_field_value('title') != strip_tags($form->get_field_value('title'))) {
			$form->set_field_error('title', 'contains_html');
			$ret = false;
		}

		return $ret;
	}


	protected function on_bad_submit() {

//		printbr('lajna');
	}


	protected function create_object() {
		$object = new TCMS_CI_Article_DBC();

		return $object;
	}


//	protected function on_trans_ending(DB_Connection $dbc) {
//
//	}


	protected function transfer_obj2form() {
		$object = $this->get_object();
		$form = $this->get_form();

		$ci = $object->get_ci();
		$title = $object->get_title();
		$content = $object->get_content();
		$intro = $object->get_intro();

		$form->set_field_value('title', $title->get_value());
		$form->set_field_value('content', $content->get_value());
		$form->set_field_value('intro', $intro->get_value());
		$form->set_field_value('created', $ci->get_created());

	}


	protected function transfer_form2obj() {
		$object = $this->get_object();
		$form = $this->get_form();

		$ci = $object->get_ci();
		$title = $object->get_title();
		$content = $object->get_content();
		$intro = $object->get_intro();

		$title->set_value($form->get_field_value('title'));
		$content->set_value($form->get_field_value('content'));
		$intro->set_value($form->get_field_value('intro'));

		if (!$ci->get_id()) {
			$dbc = $this->get_dbc();
			$ci_type = new TCMS_CI_Type_DBC();
			if ($ci_type->load_by_hid($dbc, 'article')) {
				$ci->set_type($ci_type->get_id());
			} else {
				throw new Tangra_Module_Exception('Cannot find content type with hid "article"');
			}
		}

		if ($form->get_field_value('created')) {
			$ci->set_created($form->get_field_value('created'));
		}
	}


	protected function _save_object(DB_Connection $dbc, I_DB_Storable $object) {
		$object->save($dbc, $this->get_permanent_external_param('user_id'));
	}



	protected function on_trans_ending(DB_Connection $dbc) {
		$vpt_ci_id = $this->get_permanent_external_param('vpt_ci', true);
		if ($vpt_ci_id) {
			$object = $this->get_object();
			$ci_id = $object->get_id();
			$vpage_id = $this->get_permanent_external_param('vpage');


			$vpt_ci = new TCMS_VPT_CI_DBC();
			$dbc = $this->get_dbc();
			if ($vpt_ci->load_by_id($dbc, $vpt_ci_id)) {
				if (TCMS_VPage_CI_DBC::is_unique_for_page($dbc, $vpage_id, $vpt_ci_id)) {
					$ci = new TCMS_VPage_CI_DBC();
					$ci->set_vpage($vpage_id);
					$ci->set_t_ci_id($vpt_ci_id);
					$ci->set_type($vpt_ci->get_ci_type());
					$ci->set_ci_id($ci_id);

					$ci->save($dbc);
					TCMS_VPage_DBC::update_last_modified($dbc, $vpage_id, $this->get_permanent_external_param('user_id'));
				}
			}
		}
	}
}